﻿Ext.define('SmartClient.view.OrdersHistoryView',
   {
      extend: 'Ext.window.Window',
      controller: 'SmartClient.controller.OrdersHistoryController',

      id: 'ordersHistoryWindow',
      title: 'Orders history',
      titleAlign: 'center',
      closable: false,
      resizable: false,
      draggable: false,
      hidden: true,
      modal: true,
      y: 0,
      width: 100,

      constructor: function ()
      {
         this.items[0].store = arguments[0].store;
         this.callParent(arguments);
      },

      items:
         [
            {
               xtype: 'gridpanel',
               id: '',
               sortableColumns: false,
               enableColumnHide: false,

               columns:
                  [
                     {
                        xtype: 'rownumberer',
                        align: 'center',
                        width: 25
                     },
                     {
                        text: 'Order No.',
                        dataIndex: 'id',
                        width: 80
                     },
                     {
                        text: 'Placing date',
                        dataIndex: 'placingDate',
                        width: 100
                     },
                     {
                        text: 'Products amount',
                        width: 130,
                        renderer: function (value, p, r)
                        {
                           var productsAmount = 0;
                           Ext.each(r.raw['products'], function (product, index)
                           {
                              productsAmount += product.orderedAmount;
                           });
                           return productsAmount;
                        }
                     },
                     {
                        text: 'Total price',
                        width: 85,
                        renderer: function (value, p, r)
                        {
                           var totalPrice = 0;
                           Ext.each(r.raw['products'], function (product, index)
                           {
                              totalPrice += (product.price * product.orderedAmount);
                           });
                           return totalPrice;
                        }
                     },
                     {
                        text: 'Status',
                        dataIndex: 'status',
                        width: 80
                     },
                     {
                        xtype: 'actioncolumn',
                        width: 60,
                        items:
                           [
                              {
                                 getClass: function (v, meta, rec)
                                 {
                                    if (rec.get('status') === 'New' || rec.get('status') === 'Accepted')
                                    {
                                       return 'edit-order';
                                    }
                                    else
                                    {
                                       return 'hidden-button';
                                    }
                                 },
                                 getTip: function (v, meta, rec)
                                 {
                                    if (rec.get('status') === 'New' || rec.get('status') === 'Accepted')
                                    {
                                       return 'Edit';
                                    }
                                 },
                                 handler: function (grid, rowIndex, colIndex, item, event, record)
                                 {
                                    this.up('window').getController().editOrder(grid, rowIndex, colIndex, item, event, record);
                                 }
                              },
                              {
                                 getClass: function (v, meta, rec)
                                 {
                                    if (rec.get('status') === 'New' || rec.get('status') === 'Accepted')
                                    {
                                       return 'reject-order';
                                    }
                                    else
                                    {
                                       return 'hidden-button';
                                    }
                                 },
                                 getTip: function (v, meta, rec)
                                 {
                                    if (rec.get('status') === 'New' || rec.get('status') === 'Accepted')
                                    {
                                       return 'Reject';
                                    }
                                 },
                                 handler: function (grid, rowIndex, colIndex, item, event, record)
                                 {
                                    this.up('window').getController().rejectOrder(grid, rowIndex, colIndex, item, event, record);
                                 }
                              }
                           ]
                     }
                  ],

               dockedItems:
                  [
                     {
                        xtype: 'toolbar',
                        dock: 'bottom',
                        layout:
                           {
                              type: 'hbox',
                              pack: 'center'
                           },
                        items:
                           [
                               {
                                  xtype: 'button',
                                  itemId: 'confirmButton',
                                  text: 'Confirm',
                                  width: 100
                               },
                               {
                                  xtype: 'button',
                                  text: 'Cancel',
                                  width: 100,
                                  handler: function ()
                                  {
                                     this.up('window').close();
                                  }
                               }
                           ]
                     }
                  ]
            }
         ]
   });